package com.wit.WEB.controller;

import com.wit.WEB.entity.*;
import com.wit.WEB.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: java-web-group
 * @description: 用户控制器
 * @author: KingTeng
 * @create: 2022-03-08 20:04
 **/
@Controller
public class UserController {
    private UserService userService;
    private OrderService orderService;
    private TestStandService testStandService;
    private  ResultService resultService;
    private DangerService dangerService;

    @Autowired
    public void setDangerService(DangerService dangerService){this.dangerService=dangerService;}
    @Autowired
    public void setResultService(ResultService resultService){this.resultService=resultService;}
    @Autowired
    public void setUserService(UserService userService){
        this.userService=userService;
    }
    @Autowired
    public  void setOrderService(OrderService orderService){this.orderService=orderService;}
    @Autowired
    public  void setTestStandService(TestStandService testStandService){this.testStandService=testStandService;}

    @PostMapping("/foreUUpdate")
    public String fUpdate(User user, HttpSession session) {
        System.out.println(user.getSex());
        if (user.getSex().equals("---男---")) {
            user.setSex("男");
        }
        if (user.getSex().equals("---女---")) {
            user.setSex("女");
        }
        User a= (User) session.getAttribute("user");

        user.setAccount(a.getAccount());
        user.setPower(a.getPower());

        userService.UpdateUser(user);

        session.setAttribute("user", user);
        System.out.println(user.toString());
        return "User/update";
    }

    @RequestMapping("/forListOrder")
    public String ListOrder(HttpSession session){
        User user= (User) session.getAttribute("user");
        Integer uid=user.getId();
        List<Order_> orders=orderService.findallByUid(uid);
        session.setAttribute("orders", orders);
        return "/User/ListOrder";
    }

    @RequestMapping("/OrderFindByID")
    public String FindOrder(@RequestParam(value = "oid",required = true) String oid, HttpSession session){
        session.setAttribute("oid", oid);
        Order_ order =orderService.selectOrderbyprimary(Integer.parseInt(oid));
        TestStand testStand=testStandService.selectByprimary(Integer.parseInt(order.getAddress()));
        User user= (User) session.getAttribute("user");
        Result result= resultService.selectByOid(oid);
        System.out.println(order);
        System.out.println(user);
        System.out.println(testStand);
        session.setAttribute("order", order);
        session.setAttribute("testStand",testStand);
        session.setAttribute("result", result);
        return "/User/OrderDetails";
    }

    @RequestMapping("/ToGlobalMap")
    public String globalMap(Model model)
    {
        return "/User/globalMap";
    }

    @RequestMapping("/ToChinaMap")
    public String ChinaMap(Model model)
    {
        return "/User/ChinaMap";
    }

    @RequestMapping("/ToStandOrder")
    public String StandOrder(Model model)
    {
        return "/User/StandOrder";
    }

    @RequestMapping("/ToDangerArea")
    public String DangerArea(HttpSession session){
        List<Danger> dangers=dangerService.findAll();
        session.setAttribute("dangers", dangers);
        return "/User/DangerArea";
    }
}
